﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YXShop.Web.admin.stocks
{
    public partial class Out_Stocks_Edit : System.Web.UI.Page
    {
        BLL.Stocks.BarCode bllBarCode = new BLL.Stocks.BarCode();
        BLL.Stocks.ProductTruck bllProductTruck = new BLL.Stocks.ProductTruck();
        BLL.Stocks.OutProduct bllOutProduct = new BLL.Stocks.OutProduct();
        BLL.Store.Store bllStore = new BLL.Store.Store();
        BLL.Store.StoreCenter bllStoreCenter = new BLL.Store.StoreCenter();
        BLL.Stocks.Order bllOrder = new BLL.Stocks.Order();
        BLL.Stocks.OrderInfo bllOrderInfo = new BLL.Stocks.OrderInfo();
        BLL.Product.ProductInfo bllProductinfo = new BLL.Product.ProductInfo();
        //BLL.Stocks.ProductTruck bllProductTruck = new BLL.Stocks.ProductTruck();
        protected void Page_Load(object sender, EventArgs e)
        {
            #region  初始化选择
            try
            {
               
                if (this.hfStoreCenterid0.Value.ToString() != "")
                {
                    Model.Store.StoreCenter storecenterModel = bllStoreCenter.GetModelById(int.Parse(this.hfStoreCenterid0.Value.ToString()));
                    if (storecenterModel != null)
                    {
                        this.txtStoreCenter0.Text = storecenterModel.StoreCentername;
                    }
                }
                if (this.hfstoreid0.Value.ToString() != "")
                {
                    Model.Store.Store storeModel = bllStore.GetModelById(int.Parse(this.hfstoreid0.Value.ToString()));

                    if (storeModel != null)
                    {
                        this.txtStoreName0.Text = storeModel.Storename;
                    }
                }

                if (this.hfStoreCenterid.Value.ToString() != "")
                {
                    Model.Store.StoreCenter storecenterModel = bllStoreCenter.GetModelById(int.Parse(this.hfStoreCenterid.Value.ToString()));
                    if (storecenterModel != null)
                    {
                        this.txtStoreCenter.Text = storecenterModel.StoreCentername;
                    }
                }
                if (this.hfstoreid.Value.ToString() != "")
                {
                    Model.Store.Store storeModel = bllStore.GetModelById(int.Parse(this.hfstoreid.Value.ToString()));

                    if (storeModel != null)
                    {
                        this.txtStoreName0.Text = storeModel.Storename;
                    }
                }
          
            }
            catch (Exception ex)
            {
                this.ltlMsg.Text = "出现异常--" + "\n" + ex.Message + "\n 请确认当前仓库或目标仓库选且之选一个！";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
               
            }
            #endregion
            if (!IsPostBack)
            {
                
                InitWebControl();
                if (Request.Cookies["yxInfo"] != null)
                {
                    string temp = Convert.ToString(Request.Cookies["yxInfo"].Values["orderid"]);

                    //读全部就用Request.Cookies["Info"].Value  
                    this.hfOrderid.Value = temp;
                    if (temp != "")
                    {
                        Model.Stocks.Order model = bllOrder.GetModelByOrdernume(temp);
                        if (model != null)
                        {
                            string where = " and orderid =" + model.Id;
                            this.lblList.Text = GetList(where);
                        }
                    }
                }
            }
        }
        #region 判断扫描抢仓库类型 以及仓库id

        #endregion
        #region 根据条码id 出库
        private bool OutProductByBarCode(string barcode, Model.Stocks.StockTypeEnum stockstype, int stockid, Model.Stocks.StockTypeEnum stockstype2, int stockid2, int adminid, Model.Stocks.OutTypeEnum outtype, string notes)
        {
            bool isSuccess = false;
            Model.Stocks.BarCode barcodeModel=bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel != null)
            {
                #region 判断是否与设置的仓库相符合
                int result = isMatchStock(barcode, stockstype, stockid, stockstype2, stockid2, outtype);
                #endregion
                if (result == 0)
                {
                    #region  判断商品状态 是否可以出库
                    bool isstateok = isStateok(barcode);


                    #endregion
                    if (isstateok == true)
                    {
                        //出库
                        if (outtype != Model.Stocks.OutTypeEnum.Selled)
                        {
                            OutProduct(barcode, stockstype, stockid, stockstype2, stockid2, adminid, outtype, notes);
                            isSuccess = true;
                        }
                        else
                        {
                            OutProduct(barcode, stockstype, stockid, stockstype2, stockid2, adminid, outtype, notes);
                            isSuccess = true;
                            //销售 添加平台商品处理方法代码
                        }
                    }
                    else
                    {
                        this.ltlMsg.Text = "该商品不能出库，请检查该商品状态 条码--" + barcode;
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return isSuccess;
                    }

                }
                else
                {
                    this.ltlMsg.Text = "当前仓库和目标仓库不符合出库条件，请检查仓库设置 条码--" + barcode;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return isSuccess;
                }
            }
            else
            {
                this.ltlMsg.Text = "没有找到相关商品 条码--"+barcode;
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return isSuccess;
            }
            return isSuccess;
        }
        /// <summary>
        /// 1正常出库 中央仓库--》区域中心 --》门店
        /// 2 退还 门店--》区域中心--》中央仓库
        /// 3  出售  中央仓库-->出售(网上商城) 门店-->出售
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns>0 可以出库</returns>
        private int isMatchStock(string barcode, Model.Stocks.StockTypeEnum stockstype, int stockid, Model.Stocks.StockTypeEnum stockstype2, int stockid2,Model.Stocks.OutTypeEnum outtype)
        {
            int isMatch = -1;
            Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel == null)
            {
                isMatch = 1;//没有找到该商品
            }
            else
            {
                if (!(barcodeModel.StockTypeID == stockstype && barcodeModel.StockID == stockid))
                {
                    isMatch = 2;//商品不再所设置的仓库中
                }
                else
                {
                    if (outtype == Model.Stocks.OutTypeEnum.Normal)
                    {
                        if (stockstype >= stockstype2)
                        {
                            isMatch = 3;//只能向下级出库
                        }
                        else
                        {
                            isMatch = 0;
                        }
                    }
                     //退还
                    if (outtype == Model.Stocks.OutTypeEnum.Returned)
                    {
                        if (stockstype <= stockstype2)
                        {
                            isMatch = 4;//只能向上级退货
                        }
                        else
                        {
                            isMatch = 0;
                        }
                    }
                    if (outtype == Model.Stocks.OutTypeEnum.Selled)//销售
                    {
                        if ((barcodeModel.StockTypeID == Model.Stocks.StockTypeEnum.CenterStock) || (barcodeModel.StockTypeID == Model.Stocks.StockTypeEnum.ShopStock&&barcodeModel.StockID.ToString()==this.hfstoreid0.Value))
                        {
                            isMatch = 0;
                        }
                    }
                }
            }
            return isMatch;
        }
        /// <summary>
        /// 判断商品状态 是否可以出库  入库或退还状态时候可以出库
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        private bool isStateok(string barcode)
        {
            bool isMatch = false;
            Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel == null)
            {
                isMatch = false;
            }
            else
            {
                if (barcodeModel.StateID == Model.Stocks.ProductStateEnum.Putin || barcodeModel.StateID == Model.Stocks.ProductStateEnum.Returned)
                {
                    isMatch = true;
                }
                else
                {
                    isMatch = false;
                }
            }
            return isMatch;
        }
        private int OutProduct(string barcode,Model.Stocks.StockTypeEnum stockstype,int stockid,Model.Stocks.StockTypeEnum stockstype2,int stockid2,int adminid,Model.Stocks.OutTypeEnum outtype,string notes)
        {
            int result = 0;
            Model.Stocks.BarCode barcodeModel = bllBarCode.GetModelByBarCode(barcode);
            if (barcodeModel == null)
            {
                 result=-1;
            }
            else
            {
                #region  商品出库
                try
                {
                    #region 更新商品状态
                    barcodeModel.OutTime = DateTime.Now;
                    barcodeModel.StateID = Model.Stocks.ProductStateEnum.Putout;
                    bllBarCode.Update(barcodeModel);
                    #endregion
                    #region  添加到商品跟踪表
                    Model.Stocks.ProductTruck truckModel = new Model.Stocks.ProductTruck();
                    truckModel.BarcodeID = barcodeModel.ID;
                    truckModel.OperationTime = DateTime.Now;
                    if (outtype == Model.Stocks.OutTypeEnum.Selled)
                    {
                        truckModel.OperationType = Model.Stocks.ProductStateEnum.Selled;
                    }
                    if (outtype == Model.Stocks.OutTypeEnum.Normal)
                    {
                        truckModel.OperationType = Model.Stocks.ProductStateEnum.Putout;
                    }
                    if (outtype == Model.Stocks.OutTypeEnum.Returned)
                    {
                        truckModel.OperationType = Model.Stocks.ProductStateEnum.Returned;
                    }
                    truckModel.StoreType = stockstype;
                    truckModel.StoreID = stockid;
                    truckModel.Adminid = adminid;
                    bllProductTruck.Add(truckModel);
                    #endregion
                    if (outtype != Model.Stocks.OutTypeEnum.Selled)
                    {
                        #region  添加带入库商品表
                        Model.Stocks.OutProduct outproductModel = new Model.Stocks.OutProduct();
                        outproductModel.BarcodeID = barcodeModel.ID;
                        outproductModel.Intype = (Model.Stocks.InTypeEnum)((int)outtype);
                        outproductModel.Notes = notes;
                        outproductModel.StockTypeID = stockstype2;
                        outproductModel.StockID = stockid2;
                        bllOutProduct.Add(outproductModel);
                        #endregion
                    }

                }
                catch (Exception ex)
                {
                    result = -1;
                    throw ex;
                }
                
                #endregion
            }
            return result;
        }
        #endregion
        #region 根据barcode 生成订单
        private void CreateOrder(string barcode, Model.Admin.AdminInfo admin, bool isMember, int storeId)
        {
            string orderid = "";
            //判断订单号是否为空 如果为空则生成
            if (this.hfOrderid.Value == "")
            {
                //生成订单号。添加到订单表
                orderid = DateTime.Now.ToString("yyyyMMddHHmmss");
                this.hfOrderid.Value = orderid;
                Model.Stocks.Order modelOrder = new Model.Stocks.Order();
                modelOrder.Adminid = admin.AdminId;
                modelOrder.isMember = isMember;
                modelOrder.Memberid = 0;
                modelOrder.Ordernum = orderid;
                modelOrder.Ordertime = DateTime.Now;
                modelOrder.Storeid = storeId;
                modelOrder.State = 0;
                bllOrder.Add(modelOrder);
            }
            else
            {
                //已经有订单号
                orderid = this.hfOrderid.Value;
            }
            #region
            HttpCookie cookie = new HttpCookie("yxInfo");//定义cookie对象以及名为Info的项   
            DateTime dt = DateTime.Now;//定义时间对象   
            TimeSpan ts = new TimeSpan(0, 2, 0, 0);//cookie有效作用时间，具体查msdn   
            cookie.Expires = dt.Add(ts);//添加作用时间   
            cookie.Values.Add("orderid", orderid);//增加属性
            Response.AppendCookie(cookie);//确定写入cookie中   
            #endregion

            Model.Stocks.OrderInfo modelOrderInfo = new Model.Stocks.OrderInfo();
            modelOrderInfo.Barcodeid = bllBarCode.GetModelByBarCode(barcode).ID;
            modelOrderInfo.Djnum = 0;//得到点卷数
            modelOrderInfo.Orderid = bllOrder.GetModelByOrdernume(orderid).Id;
            modelOrderInfo.Price = bllProductinfo.GetModel(bllBarCode.GetModelByBarCode(barcode).Productid).pro_ShopPrice;
            bllOrderInfo.Add(modelOrderInfo);
        }
        #endregion
        #region 加入订单
       
        public void addOrder(string barcode,bool isMember)
        {
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            int storeId = int.Parse(hfstoreid0.Value);
            if (isStateok(barcode))//状态可以加入订单
            {
                if (isMatchStock(barcode, (Model.Stocks.StockTypeEnum)(int.Parse(this.DropDownListNowStockType.SelectedValue)), int.Parse(this.hfstoreid0.Value), (Model.Stocks.StockTypeEnum)(0), 0, (Model.Stocks.OutTypeEnum)2) == 0) ;
                {
                    int outtype = int.Parse(this.DropDownListOutType.SelectedItem.Value);
                    if (outtype == 2)//销售
                    {
                        IList<string> barcodeoder = new List<string>();
                        if (this.hfOrderid.Value != "")
                        {
                            IList<Model.Stocks.OrderInfo> infos = bllOrderInfo.GetModelsByOrderId(bllOrder.GetModelByOrdernume(this.hfOrderid.Value).Id);
                            foreach (Model.Stocks.OrderInfo info in infos)
                            {
                                barcodeoder.Add(bllBarCode.GetModelById((int)info.Barcodeid).Barcode);
                            }
                        }
                        if (!barcodeoder.Contains(barcode))
                        {
                            CreateOrder(barcode, admin, isMember, storeId);
              
                        }
                        else
                        {
                            this.ltlMsg.Text = "订单已经存在，请勿重复添加";
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionErr";
                            return;
                        }
                    }
                }
            }
            else
            {
 
            }
        }
        #endregion
        #region 出库按钮点击事件
        protected void btnOutProduct_Click(object sender, EventArgs e)
        {
            try
            {
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if (admin != null)
                {
                    if (this.DropDownListOutType.SelectedItem.Value != "2")
                    {
                        string barcode = this.txtBarCode.Text.Trim();

                        Model.Stocks.OutTypeEnum outtype = (Model.Stocks.OutTypeEnum)(int.Parse(this.DropDownListOutType.SelectedItem.Value));
                        Model.Stocks.StockTypeEnum stockstype = (Model.Stocks.StockTypeEnum)(int.Parse(DropDownListNowStockType.SelectedItem.Value));
                        Model.Stocks.StockTypeEnum stockstype2 = (Model.Stocks.StockTypeEnum)(int.Parse(DropDownListOutStockType.SelectedItem.Value));
                        int stockid = 0;
                        int stockid2 = 0;
                        if (stockstype == Model.Stocks.StockTypeEnum.AreaStock)
                        {
                            if (hfStoreCenterid0.Value.ToString() != "")
                            {
                                stockid = int.Parse(hfStoreCenterid0.Value.ToString());
                            }
                            else
                            {
                                stockid = -1;
                            }
                        }
                        if (stockstype == Model.Stocks.StockTypeEnum.ShopStock)
                        {
                            if (hfstoreid0.Value.ToString() != "")
                            {
                                stockid = int.Parse(hfstoreid0.Value.ToString());
                            }
                            else
                            {
                                stockid = -1;
                            }
                        }
                        if (stockstype == Model.Stocks.StockTypeEnum.CenterStock)
                        {
                            stockid = 0;
                        }
                        if (stockstype2 == Model.Stocks.StockTypeEnum.AreaStock)
                        {
                            if (hfStoreCenterid.Value.ToString() != "")
                            {
                                stockid2 = int.Parse(hfStoreCenterid.Value.ToString());
                            }
                            else
                            {
                                stockid2 = -1;
                            }
                        }
                        if (stockstype2 == Model.Stocks.StockTypeEnum.ShopStock)
                        {
                            if (hfstoreid.Value.ToString() != "")
                            {
                                stockid2 = int.Parse(hfstoreid.Value.ToString());
                            }
                            else
                            {
                                stockid2 = -1;
                            }
                        }
                        if (stockstype2 == Model.Stocks.StockTypeEnum.CenterStock)
                        {
                            stockid2 = 0;
                        }
                        if (this.DropDownListOutStockType.SelectedItem.Value == "2")//出售
                        {
                            if (stockid >= 0)
                            {
                                OutProductByBarCode(barcode, stockstype, stockid, stockstype2, stockid2, admin.AdminId, outtype, this.txtNotes.Value);
                                this.ltlMsg.Text = "出库成功：条码编号--" + barcode;
                                this.pnlMsg.Visible = true;
                                this.pnlMsg.CssClass = "actionOk";
                            }
                            else
                            {
                                this.ltlMsg.Text = "请选择当前仓库";
                                this.pnlMsg.Visible = true;
                                this.pnlMsg.CssClass = "actionErr";
                                return;
                            }
                        }
                        else
                        {
                            if (stockid >= 0 && stockid2 >= 0)
                            {
                                bool isSuccess = OutProductByBarCode(barcode, stockstype, stockid, stockstype2, stockid2, admin.AdminId, outtype, this.txtNotes.Value);
                                if (isSuccess)
                                {
                                    this.ltlMsg.Text = "出库成功：条码编号--" + barcode;
                                    this.pnlMsg.Visible = true;
                                    this.pnlMsg.CssClass = "actionOk";
                                    this.txtBarCode.Text = "";
                                    this.hfOrderid.Value = "";//订单号清空
                                }
                            }
                            else
                            {
                                this.ltlMsg.Text = "请选择仓库当前仓库或目标仓库";
                                this.pnlMsg.Visible = true;
                                this.pnlMsg.CssClass = "actionErr";
                                return;
                            }
                        }

                    }
                    else //售出
                    {
                        Model.Stocks.Order modelOrder = bllOrder.GetModelByOrdernume(this.hfOrderid.Value);
                        if (modelOrder != null)
                        {
                            IList<Model.Stocks.OrderInfo> modelsInfo = bllOrderInfo.GetModelsByOrderId(modelOrder.Id);
                            if (modelsInfo.Count > 0)
                            {
                                foreach (Model.Stocks.OrderInfo info in modelsInfo)
                                {
                                    string barcode = bllBarCode.GetModelById((int)info.Barcodeid).Barcode;
                                    Model.Stocks.OutTypeEnum outtype = (Model.Stocks.OutTypeEnum)(int.Parse(this.DropDownListOutType.SelectedItem.Value));
                                    Model.Stocks.StockTypeEnum stockstype = (Model.Stocks.StockTypeEnum)(int.Parse(DropDownListNowStockType.SelectedItem.Value));
                                    Model.Stocks.StockTypeEnum stockstype2 = (Model.Stocks.StockTypeEnum)(int.Parse(DropDownListOutStockType.SelectedItem.Value));
                                    int stockid = 0;
                                    int stockid2 = 0;
                                    if (stockstype == Model.Stocks.StockTypeEnum.AreaStock)
                                    {
                                        if (hfStoreCenterid0.Value.ToString() != "")
                                        {
                                            stockid = int.Parse(hfStoreCenterid0.Value.ToString());
                                        }
                                        else
                                        {
                                            stockid = -1;
                                        }
                                    }
                                    if (stockstype == Model.Stocks.StockTypeEnum.ShopStock)
                                    {
                                        if (hfstoreid0.Value.ToString() != "")
                                        {
                                            stockid = int.Parse(hfstoreid0.Value.ToString());
                                        }
                                        else
                                        {
                                            stockid = -1;
                                        }
                                    }
                                    if (stockstype == Model.Stocks.StockTypeEnum.CenterStock)
                                    {
                                        stockid = 0;
                                    }
                                    if (stockstype2 == Model.Stocks.StockTypeEnum.AreaStock)
                                    {
                                        if (hfStoreCenterid.Value.ToString() != "")
                                        {
                                            stockid2 = int.Parse(hfStoreCenterid.Value.ToString());
                                        }
                                        else
                                        {
                                            stockid2 = -1;
                                        }
                                    }
                                    if (stockstype2 == Model.Stocks.StockTypeEnum.ShopStock)
                                    {
                                        if (hfstoreid.Value.ToString() != "")
                                        {
                                            stockid2 = int.Parse(hfstoreid.Value.ToString());
                                        }
                                        else
                                        {
                                            stockid2 = -1;
                                        }
                                    }
                                    if (stockstype2 == Model.Stocks.StockTypeEnum.CenterStock)
                                    {
                                        stockid2 = 0;
                                    }
                                    if (this.DropDownListOutStockType.SelectedItem.Value == "2")//出售
                                    {
                                        if (stockid >= 0)
                                        {
                                            OutProductByBarCode(barcode, stockstype, stockid, stockstype2, stockid2, admin.AdminId, outtype, this.txtNotes.Value);
                                            Model.Stocks.Order orderModel=bllOrder.GetModelByOrdernume(this.hfOrderid.Value);
                                            orderModel.State=1;//更改订单状态 为 1
                                            bllOrder.Update(orderModel);
                                            this.ltlMsg.Text = "出库成功：条码编号--" + barcode;
                                            this.pnlMsg.Visible = true;
                                            this.pnlMsg.CssClass = "actionOk";
                                        }
                                        else
                                        {
                                            this.ltlMsg.Text = "请选择当前仓库";
                                            this.pnlMsg.Visible = true;
                                            this.pnlMsg.CssClass = "actionErr";
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        if (stockid >= 0 && stockid2 >= 0)
                                        {
                                            bool isSuccess = OutProductByBarCode(barcode, stockstype, stockid, stockstype2, stockid2, admin.AdminId, outtype, this.txtNotes.Value);
                                            if (isSuccess)
                                            {
                                                this.ltlMsg.Text = "出库成功：条码编号--" + barcode;
                                                this.pnlMsg.Visible = true;
                                                this.pnlMsg.CssClass = "actionOk";
                                                this.txtBarCode.Text = "";
                                                this.hfOrderid.Value = "";//订单号清空
                                            }
                                        }
                                        else
                                        {
                                            this.ltlMsg.Text = "请选择仓库当前仓库或目标仓库";
                                            this.pnlMsg.Visible = true;
                                            this.pnlMsg.CssClass = "actionErr";
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    this.ltlMsg.Text = "用户操作失败，请重新登录";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }

            }
            catch (Exception ex)
            {
                this.ltlMsg.Text = "出错：\n" + ex.Message + "\n 请确认当前仓库和目标仓库选且之选一个！";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }
            clear();//清楚数据
        }
        #endregion
        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            //ChangeHope.WebPage.WebControl.Validate(this.txtBarCode, "条形码不能为空", "isnull", "必填", "该项为必填项");
            this.txtStoreName0.Attributes.Add("readonly", "readonly");
            this.txtStoreName0.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfstoreid0.ClientID + "," + this.txtStoreName0.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtStoreName.Attributes.Add("readonly", "readonly");
            this.txtStoreName.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfstoreid.ClientID + "," + this.txtStoreName.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtStoreCenter0.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter0.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid0.ClientID + "," + this.txtStoreCenter0.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtStoreCenter.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid.ClientID + "," + this.txtStoreCenter.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion
        protected void DropDownListOutType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownListOutType.SelectedItem.Value == "0")//出库
            {
                //DropDownListOutStockType.SelectedIndex = 1;
                this.DropDownListOutStockType.Enabled = true;
                this.txtCenter.Enabled = true;
                this.txtStoreCenter.Enabled = true;
                this.txtStoreName.Enabled = true;
                this.btnOutProduct1.Text = "出库";
                this.txtCenter.Enabled = true;
                this.div2.Visible = false;
            }
            if (DropDownListOutType.SelectedItem.Value == "1")//退货
            {
                this.div2.Visible = false;
                //DropDownListNowStockType.SelectedIndex = 1;
                //退货不需要选择出库，从哪里进来的退到哪里
                this.DropDownListOutStockType.Enabled = false;
                this.txtCenter.Enabled = false;
                this.txtStoreCenter.Enabled = false;
                this.txtStoreName.Enabled = false;
                this.btnOutProduct1.Text = "退货";
                if (txtBarCode.Text.Trim() != "")
                {
                    Model.Stocks.ProductTruck truck = GetByBarCode(this.txtBarCode.Text.Trim());
                    if (truck == null)
                    { }
                    else
                    {
                        int stocktype = (int)truck.StoreType;
                        int stockid = truck.StoreID;
                        this.DropDownListOutStockType.SelectedValue = stocktype.ToString();
                        //this.DropDownListOutStockType.Text = this.DropDownListOutStockType.Items.FindByValue(stocktype.ToString()).Text;
                        if (stocktype == 0)
                        {
                            this.txtCenter.Visible = true;
                            this.txtStoreCenter.Visible = false;
                            this.txtStoreName.Visible = false;
                        }
                        if (stocktype == 1)
                        {
                            this.txtCenter.Visible = false;
                            this.txtStoreCenter.Visible = true;
                            this.txtStoreName.Visible = false;
                            this.hfStoreCenterid.Value = stockid.ToString();
                            this.txtStoreCenter.Text = bllStoreCenter.GetModelById(stockid).StoreCentername;
                        }
                        if (stocktype == 2)
                        {
                            this.txtCenter.Visible = false;
                            this.txtStoreCenter.Visible = false;
                            this.txtStoreName.Visible = true;
                            this.hfstoreid.Value = stockid.ToString();
                            this.txtStoreName.Text = bllStore.GetModelById(stockid).Storename;
                        }
                    }
                }

            }
            if (DropDownListOutType.SelectedItem.Value == "2")//销售
            {
                
                this.txtCenter.Enabled = true;
                this.txtStoreCenter.Enabled = true;
                this.txtStoreName.Enabled = true;
                this.btnOutProduct1.Text = "售出";
                this.DropDownListOutStockType.Enabled = false;
                this.txtCenter.Enabled = false;
                this.div2.Visible = true;
            }
        }
        #region 根据条码 获取商品的来源  （退货调用）
        public Model.Stocks.ProductTruck GetByBarCode(string barCode)
        {
            Model.Stocks.ProductTruck  truckModel= null;
            #region 商品必须是入库状态 
           Model.Stocks.BarCode barCodeModel=bllBarCode.GetModelByBarCode(barCode);
           if (barCodeModel == null)
           {
               return null;
           }
           else
           {
               int barcodeid = barCodeModel.ID;
              List<Model.Stocks.ProductTruck> listTurck= bllProductTruck.GetListProductTruckByWhere("barcodeid ="+ barcodeid.ToString());
              if (listTurck == null)
              {
                  return null;
              }
              else
              {
                  if (listTurck.Count < 3) //可以退货  至少有3条跟踪记录
                  {
                      return null;
                  }
                  else
                  {
                      Model.Stocks.ProductTruck tModel = listTurck[listTurck.Count - 1];//最后一条记录
                      if (tModel.OperationType == Model.Stocks.ProductStateEnum.Putin)//入库
                      {
                          if (tModel.StoreType > 0)
                          {
                              int storetype = (int)tModel.StoreType;
                              int backStorType = storetype - 1;//退到上级
                              for (int i = listTurck.Count - 1; i > 0; i--)
                              {
                                  if ((int)listTurck[i].StoreType == backStorType)
                                  {
                                      truckModel = listTurck[i];
                                      return truckModel;
                                  }
                              }
                          }
                          else  //在中央仓库
                          {
                              return null;
                          }
                          //return truckModel=listTurck[listTurck.Count - 3];//可退货商品来源数据对象
                      }
                      else //不能退货
                      {
                          return null;
                      }
                  }
              }
           }

            #endregion
            return truckModel;
        }
        #endregion 
        #region 订单
        private string GetList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = bllOrderInfo.GetList(where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("", "订单号");
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "价格");
            table.AddHeadCol("", "获点卷数");
            table.AddHeadCol("", "会员名");
            table.AddHeadCol("", "操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    Model.Stocks.OutProduct outProductModel = bllOutProduct.GetModelById(int.Parse(dataPage.DataReader["id"].ToString()));
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["id"].ToString() + "\" />");
                    int orderinfoid = int.Parse(dataPage.DataReader["id"].ToString());
                    Model.Stocks.OrderInfo modelOrderInfo = bllOrderInfo.GetModelById(orderinfoid);
                    int orderId = (int)modelOrderInfo.Orderid;
                    Model.Stocks.Order modelOrder=bllOrder.GetModelById(orderId);
                    table.AddCol(modelOrder.Ordernum);
                    Model.Stocks.BarCode modelBarCode = bllBarCode.GetModelById((int)modelOrderInfo.Barcodeid);
                    int productid = modelBarCode.Productid;
                    Model.Product.ProductInfo productModel = bllProductinfo.GetModel(productid);
                    table.AddCol(productModel.pro_Name);
                    table.AddCol(modelOrderInfo.Price.ToString());
                    table.AddCol(modelOrderInfo.Djnum.ToString());
                    table.AddCol(modelOrder.Memberid.ToString());
                    table.AddCol(string.Format("<a href='#' onclick='Del({0})'>删除</a>", productid));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion
        protected void DropDownListNowStockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfstoreid0.Value = "";
            this.hfStoreCenterid0.Value = "";
            if (DropDownListNowStockType.SelectedItem.Value == "0")//中央仓库
            {
                this.txtCenter0.Visible = true;
                this.txtStoreCenter0.Visible = false;
                this.txtStoreName0.Visible = false;
            }
            if (DropDownListNowStockType.SelectedItem.Value == "1")//区域中心
            {
                this.txtCenter0.Visible = false;
                this.txtStoreCenter0.Visible = true;
                this.txtStoreName0.Visible = false;
         
            }
            if (DropDownListNowStockType.SelectedItem.Value == "2")//门店
            {
                this.txtCenter0.Visible = false;
                this.txtStoreCenter0.Visible = false;
                this.txtStoreName0.Visible = true;
              
            }
         
        }

        protected void DropDownListOutStockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfstoreid.Value = "";
            this.hfStoreCenterid.Value = "";
            if (DropDownListOutStockType.SelectedItem.Value == "0")//中央仓库
            {
                this.txtCenter.Visible = true;
                this.txtStoreCenter.Visible = false;
                this.txtStoreName.Visible = false;
            }
            if (DropDownListOutStockType.SelectedItem.Value == "1")//区域中心
            {
                this.txtCenter.Visible = false;
                this.txtStoreCenter.Visible = true;
                this.txtStoreName.Visible = false;
            }
            if (DropDownListOutStockType.SelectedItem.Value == "2")//门店
            {
                this.txtCenter.Visible = false;
                this.txtStoreCenter.Visible = false;
                this.txtStoreName.Visible = true;
            }
        }

        protected void txtBarCode_TextChanged(object sender, EventArgs e)
        {
            if (this.DropDownListOutType.SelectedValue == "1")//退货
            {
                if (txtBarCode.Text.Trim() != "")
                {
                    Model.Stocks.ProductTruck truck = GetByBarCode(this.txtBarCode.Text.Trim());
                    if (truck == null)
                    { }
                    else
                    {
                        int stocktype = (int)truck.StoreType;
                        int stockid = truck.StoreID;
                        this.DropDownListOutStockType.SelectedValue = stocktype.ToString();
                        string ss = this.DropDownListOutStockType.Items.FindByValue(stocktype.ToString()).Text;
                           //this.DropDownListOutType.Text=this.DropDownListOutStockType.Items.FindByValue(stocktype.ToString()).Text;
                        
                        if (stocktype == 0)
                        {
                            this.txtCenter.Visible = true;
                            this.txtStoreCenter.Visible = false;
                            this.txtStoreName.Visible = false;
                        }
                        if (stocktype == 1)
                        {
                            this.txtCenter.Visible = false;
                            this.txtStoreCenter.Visible = true;
                            this.txtStoreName.Visible = false;
                            this.hfStoreCenterid.Value = stockid.ToString();
                            this.txtStoreCenter.Text=bllStoreCenter.GetModelById(stockid).StoreCentername;
                        }
                        if (stocktype == 2)
                        {
                            this.txtCenter.Visible = false;
                            this.txtStoreCenter.Visible = false;
                            this.txtStoreName.Visible = true;
                            this.hfstoreid.Value = stockid.ToString();
                            this.txtStoreName.Text = bllStore.GetModelById(stockid).Storename;
                        }
                    }
                }
            }
        }

        protected void btnAddOrder_Click(object sender, EventArgs e)
        {
            if(this.txtBarCode.Text.Trim()!="")
            {
                addOrder(this.txtBarCode.Text.Trim(),false);
            }

            this.lblList.Text = GetList(" and orderid =" + bllOrder.GetModelByOrdernume(this.hfOrderid.Value).Id.ToString());
            this.lbprice.Text = bllOrderInfo.GetPriceByOrderId(bllOrder.GetModelByOrdernume(this.hfOrderid.Value).Id).ToString();
        }

        protected void btnCreateBarCode_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }
        /// <summary>
        ///  需要清楚的数据 cookies 等
        /// </summary>
        public void clear()
        {
            if(this.hfOrderid.Value != "")
            {
                Model.Stocks.Order model = bllOrder.GetModelByOrdernume(this.hfOrderid.Value);
                if (model != null)
                {
                    if (model.State != 1)//状态不为 1  删除信息
                    {
                        IList<Model.Stocks.OrderInfo> infos = bllOrderInfo.GetModelsByOrderId(model.Id);
                        foreach (Model.Stocks.OrderInfo info in infos)
                        {
                            bllOrderInfo.Delete(info.Id);
                        }
                        bllOrder.Delete(model.Id);
                    }
                }
            }
            if (Request.Cookies["yxInfo"] != null)
            {
                Response.Cookies["yxInfo"]["orderid"] = "";//清空cookies
                this.hfOrderid.Value = "";
            }

        }

 
    }
}